package com.shynin.oauth.service;

import com.shynin.common.bean.dto.UserDTO;
import com.shynin.oauth.entity.UserDetailsImpl;
import com.shynin.service.IUserService;
import lombok.extern.log4j.Log4j;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.stereotype.Service;

import java.util.Objects;


/**
 * Created by Shynin on 2019/1/31.
 */
@Slf4j
@Service
public class UserDetailsServiceImpl implements UserDetailsService {

    private static final long serialVersionUID = 1L;

    @Autowired
    private IUserService userService;

    @Override
    public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
//        UserDTO userDTO = userService.findByName(username);
        UserDTO userDTO = new UserDTO(2,"jhon", "$2a$10$2KFSEasI2W.zolUaIJJXneTpbhEoz8za1nGYnKvjllSAXmT9wvI0m", "18300000001");
        if(Objects.isNull(userDTO)) {
            log.error("username: {}", username);
            throw new UsernameNotFoundException("failed to get admin user info");
        }

        return new UserDetailsImpl(userDTO);
    }
}
